<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.5"/>
<title>NDK Programmer&#39;s Guide: Importing Modules (Sharing Code Made Easy)</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">NDK Programmer&#39;s Guide
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.5 -->
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;"
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('md_4__additional__info__i_m_p_o_r_t-_m_o_d_u_l_e.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Importing Modules (Sharing Code Made Easy) </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Starting from r5, the Android NDK comes with a cool feature that allows you to share and reuse other people's modules more easily.</p>
<h2>Overview:</h2>
<p>The main idea behind this feature are:</p>
<ul>
<li>You can install NDK modules outside of your main project source tree.</li>
<li>You can easily 'import' them into your project with a one-line command.</li>
</ul>
<p>In practice, here's how this works:</p>
<ol type="1">
<li><p class="startli">Your NDK_MODULE_PATH environment variable will contain a list of search paths on your system to lookup for modules.</p>
<p class="startli">It is up to you to set the variable, and to copy other modules to the directories you listed in it.</p>
</li>
<li><p class="startli">To import a module, place a line like the following to, preferably at the <em>end</em> of, your Android.mk: </p>
<pre class="fragment"> $(call import-module,&lt;tag&gt;)
</pre><p class="startli">This will look for &lt;tag&gt;/Android.mk under any of the directories listed in your NDK_MODULE_PATH.</p>
<p class="startli">(The reason why it must be at the end is to avoid messing with the results of the 'my-dir' function. See its description in docs/ANDROID-MK.html for details).</p>
</li>
<li>Declare that your project's modules depend on the imported one by listing them in either your LOCAL_STATIC_LIBRARIES or LOCAL_SHARED_LIBRARIES. For example: <pre class="fragment"> LOCAL_STATIC_LIBRARIES += &lt;tag&gt;
</pre></li>
<li><p class="startli">Rebuild!</p>
<p class="startli">Remember that NDK r5 also added the ability for a module to "export" declarations to other modules that depend on it (for example, see the definition of LOCAL_EXPORT_CFLAGS in docs/ANDROID-MK.html).</p>
<p class="startli">A well-written module will correctly export all the things its dependees need, et voila.</p>
</li>
</ol>
<p>Now for the full details:</p>
<h2><code>NDK_MODULE_PATH</code></h2>
<p>The NDK_MODULE_PATH variable must contain a list of directories.</p>
<ul>
<li>Due to GNU Make limitations, NDK_MODULE_PATH must not contain any space. The NDK will complain if this is not the case.</li>
<li>Use ':' as the path separator.</li>
<li>On Windows, use '/' as the directory separator.</li>
</ul>
<p>The directories of NDK_MODULE_PATH will be searched in order. The first &lt;path&gt;/&lt;tag&gt;/Android.mk file that is found during the lookup will be included automatically.</p>
<p>As a convenience, $NDK/sources is appended to your NDK_MODULE_PATH definition by the NDK build system. This allows you to easily import the helper libraries that come with it (see docs/CPU-FEATURES.html for a practical example).</p>
<h2>Writing an import module</h2>
<p>Writing an import module is trivial and very similar to what you do when writing project modules:</p>
<ol type="1">
<li><p class="startli">Create a sub-directory from one of your NDK_MODULE_PATH directories.</p>
<p class="startli">For example, if NDK_MODULE_PATH is defined to /home/user/ndk-modules, then create the directory /home/user/ndk-modules/my-module/</p>
</li>
<li><p class="startli">Place an Android.mk and eventual source code there.</p>
<p class="startli">Just like you would for a project module, where these files normally go to $PROJECT_PATH/Android.mk. In the example above, this would go to /home/user/ndk-modules/my-module/Android.mk</p>
<p class="startli">NOTE: Any Application.mk file here will be ignored.</p>
</li>
<li>Any module that depends on your new module, would import by calling the import-module function. For example: <pre class="fragment">  $(call import-module,my-first-module)
</pre></li>
</ol>
<p>Import modules <em>can</em> import other modules, but circular dependencies are not permitted and will be detected. Dependencies are transitive and the build system will compute all the things that need to be built for you.</p>
<p>The NDK build system will not place object files or executables in your import module directory (they will all be placed under the project's build directory, e.g. $PROJECT_PATH/obj/).</p>
<p>You can however distribute prebuilt binaries in your import module with the new PREBUILT_STATIC_LIBRARIES or PREBUILT_SHARED_LIBRARIES feature (see docs/ANDROID-MK.html).</p>
<p>This makes it easy to package and redistribute your import module directory to third-parties.</p>
<h2>Naming an import module</h2>
<p>It is important to understand a few things related to the naming of your import module:</p>
<ul>
<li><p class="startli">What 'import-module' does is really search for a file named Android.mk using the list provided by NDK_MODULE_PATH, then include while performing very little bit of house-keeping.</p>
<p class="startli">Your imported Android.mk can define any number of modules, with any name. As a consequence, there is no direct relationship between &lt;name&gt; in the following line: </p>
<pre class="fragment">  $(call import-module,&lt;tag&gt;/&lt;name&gt;)
</pre><p class="startli">And the names of the modules defined under &lt;tag&gt;/&lt;name&gt;/Android.mk.</p>
<p class="startli">IN CASE OF DOUBT, KEEP IT SIMPLE!</p>
<p class="startli">If you only plan to provide one import module, just name it like the base import directory.</p>
<p class="startli">On the other hand, you may want to provide a static and a shared version of your module: use distinct names under the same top-level Android.mk. Consider the following build script: </p>
<pre class="fragment">$NDK_MODULE_PATH/foo/bar/Android.mk:

   LOCAL_PATH := $(call my-dir)

   # Static version of the library is named 'bar_static'
   include $(CLEAR_VARS)
   LOCAL_MODULE := bar_static
   LOCAL_SRC_FILES := bar.c
   # Ensure our dependees can include &lt;bar.h&gt; too
   LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
   include $(BUILD_STATIC_LIBRARY)

   # Shared version of the library is named 'bar_shared'
   LOCAL_MODULE := bar_shared
   LOCAL_SRC_FILES := bar.c
   LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
   include $(BUILD_SHARED_LIBRARY)
</pre><p class="startli">Another module would refer to it by doing the following: </p>
<pre class="fragment"> 1. Import 'foo/bar', as in:

          $(call import-module,foo/bar)

 2. To use the static library:

        ...
        LOCAL_STATIC_LIBRARIES := bar_static

 3. Or to use the shared library:

        ...
        LOCAL_SHARED_LIBRARIES := bar_shared
</pre><ul>
<li><p class="startli">The module namespace is flat, so try to give your modules names that are likely to not collide with other. Note that your can use LOCAL_MODULE_FILENAME to give the name of your module's binary file, independently from its LOCAL_MODULE (see docs/ANDROID-MK.html for definition and usage). For example: </p>
<pre class="fragment">    include $(CLEAR_VARS)
    LOCAL_MODULE := super_foo
    LOCAL_MODULE_FILENAME := foo   # will give libfoo.so
    LOCAL_SRC_FILES := foo-src.c
    LOCAL_CFLAGS := -DVOLUME=11
    include $(BUILD_SHARED_LIBRARY)

    include $(CLEAR_VARS)
    LOCAL_MODULE := normal_foo
    LOCAL_MODULE_FILENAME := foo   # will also give libfoo.so
    LOCAL_SRC_FILES := foo-src.c
    include $(BUILD_SHARED_LIBRARY)
</pre><p class="startli">Defines two modules named "super_foo" and "normal_foo" which both produce a shared library named 'libfoo.so'</p>
<p class="startli">As a consequence, only one of them can be used by your project or a conflict will happen at build time. This allows you to select either the normal or optimized version in your NDK build scripts, while keeping the same simple loading instruction in your Java sources as: </p>
<pre class="fragment">  static {
      System.loadLibrary("foo");
  }
</pre></li>
</ul>
</li>
</ul>
<h2>Tips &amp; Recommendations</h2>
<ul>
<li>You don't need to import a module before you can reference it!</li>
<li>Use import-module at the <em>end</em> of your Android.mk to avoid messing with the result of 'my-dir'. See the description of this function in docs/ANDROID-MK.html to understand why.</li>
<li><p class="startli">It is <em>strongly</em> suggested to use a subdirectory for your import tags, that describes its origin, as in: </p>
<pre class="fragment">  $(call import-module,gtk/glib)
</pre><p class="startli">or something like: </p>
<pre class="fragment">     $(call import-module,com.example/awesomelib)
</pre><p class="startli">IMPORTANT: THE 'android' IMPORT DIRECTORY, AND ANY OF ITS SUB-DIRECTORIES IS <em>RESERVED</em> FOR NDK USAGE. FEEL FREE TO ORGANIZE YOUR OTHER IMPORT MODULES AS YOU WANT OTHERWISE. </p>
</li>
</ul>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Wed Jun 25 2014 00:51:19 for NDK Programmer&#39;s Guide by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li>
  </ul>
</div>
</body>
</html>
